<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User_amount_model extends CI_Model {
	function __construct(){
        parent::__construct();
    }
    
	function arrays($key){
    	$data = array(
	    	'amount_status'		=> array("0"=>"待审核","1"=>"审核通过","2"=>"审核未通过"),
			'amount_type'		=> array("1"=>"借款信用额度","2"=>"借款担保额度","3"=>"借款抵押额度"),
    	);
    	if($key && isset($data[$key])){
    		return $data[$key];
    	}else{
    		return array();
    	}
    }
    
    function add_amount($data){
		$this->db->insert('rsh_user_amount', $data);
		return $this->db->affected_rows();
    }
    
	function add_amount_apply($data){
		$this->db->insert('rsh_user_amount_apply', $data);
		return $this->db->affected_rows();
    }
    
	function add_user_amount_log($data){
		$this->db->insert('rsh_user_amount_log', $data);
		return $this->db->affected_rows();
    }
    
    function update($data,$where){
    	if(!empty($where)){
    		$this->db->where($where);
    	}
		$this->db->update('rsh_user_amount', $data);
		return $this->db->affected_rows();
    }
    
	function update_amount_apply($data,$where){
    	if(!empty($where)){
    		$this->db->where($where);
    	}
		$this->db->update('rsh_user_amount_apply', $data);
		return $this->db->affected_rows();
    }
    
    function get_one($user_id,$select='*'){
    	$this->db->select($select);
		$this->db->where('user_id', $user_id);
		$query = $this->db->get('rsh_user_amount');
		$result = $query->row_array();
		if ($result == false){
			$this->db->insert('rsh_user_amount', array('user_id'=>$user_id));
			$result['credit'] 		= 0;
			$result['credit_use'] 	= 0;
			$result['credit_nouse'] = 0;
		}
		return $result;
	}
	
	function get_amount_apply_obj($id,$select='*'){
    	$this->db->select($select);
		$this->db->where('id', $id);
		$query = $this->db->get('rsh_user_amount_apply');
		return $query->row_array();
	}
	
	function get_last_user_amount_apply($data = array()){		
		//封装查询内容
		$_select = "fstatus";
		
		//封装条件
		$_where = " where 1=1 and account>0";
		$_limit = "";
		if(isset($data['where']['user_id']) && $data['where']['user_id'] != ''){
			$_where .= " and user_id = ".$data['where']['user_id'];
		}
		$_limit = " limit 0, 1";
		$_order = " order by id desc ";
		//封装sql
		$sql_result = "select $_select from rsh_user_amount_apply";
		$sql_result .= $_where;
		$sql_result .= $_order;
		$sql_result .= $_limit;
		$query = $this->db->query($sql_result);
		return $query->row_array();
	}
	
	function get_amount_apply_list($data = array()){		
		//封装查询内容
		$_select = "t1.id,t1.user_id,t1.ftype,t1.account,t1.account_new,t1.account_old,
					t1.fstatus,t1.remark,t1.add_time,t2.username,t1.verify_remark";
		
		//封装条件
		$_where = " where 1=1 ";
		$_limit = "";
		if(isset($data['where']['user_id']) && $data['where']['user_id'] != ''){
			$_where .= " and t1.user_id = ".$data['where']['user_id'];
		}
		if (isset($data['page_size'])){
			$_limit = " limit ".$data['page_start'].",".$data['page_size'];
		}
		$_order = " order by t1.id desc ";
		//封装sql
		$sql_result = "select $_select from rsh_user_amount_apply as t1 left join rsh_user as t2 on t1.user_id=t2.user_id ";
		$sql_result .= $_where;
		$sql_result .= $_order;
		$sql_result .= $_limit;
		$query = $this->db->query($sql_result);
		$data['amount_apply_list'] = $query->result_array();
		
		//总记录条数
		$sql_count = "select count(*) as count from rsh_user_amount_apply as t1 left join rsh_user as t2 on t1.user_id=t2.user_id ";
		$sql_count .= $_where;
		$query = $this->db->query($sql_count);
		$data['amount_apply_count'] = $query->row()->count;
		return $data;
	}
	
	function add_amount_log($data){
		$id 			= $data['id'];
		$admin_user_id 	= $data['admin_user_id'];
		$fstatus 		= $data['fstatus'];
		$remark  		= $data['verify_remark'];	
		$amount_apply_obj = self::get_amount_apply_obj($id);
		
		/*tangc 死锁问题*/
		$this->db->trans_strict(FALSE);
    	$this->db->trans_begin();
		if($fstatus == 1){
			//添加额度记录
			$_result = self::get_one($amount_apply_obj['user_id']);
			$_data["user_id"] 		= $amount_apply_obj['user_id'];
			$_data["ftype"] 		= AMOUNT_APPLY_ADD;
			$_data["amount_type"] 	= AMOUNT_CREDIT;
			$_data["account"] 		= $amount_apply_obj['account'];
			$_data["account_all"] 	= $_data["account"];
			$_data["account_use"] 	= $_data["account"];
			$_data["account_nouse"] = $_result['credit_nouse'];
			$_data["remark"] 		= "申请额度审核通过";
    	
			$this->db->insert('rsh_user_amount_log', $_data);
			//更新信息
			$this->db->query("update rsh_user_amount_apply set fstatus={$fstatus},verify_time=".time().",verify_user={$admin_user_id},verify_remark='{$remark}',account_new='{$_data["account_all"]}' where id = {$id}");
			$this->db->query("update rsh_user_amount set credit={$_data["account_all"]},credit_use={$_data["account_use"]},credit_nouse={$_data["account_nouse"]} where user_id={$amount_apply_obj['user_id']}");
		}else{
			$this->db->query("update rsh_user_amount_apply set fstatus={$fstatus},verify_time=".time().",verify_user={$admin_user_id},verify_remark='{$remark}' where id = {$id}");
		}
		if ($this->db->trans_status() === TRUE) {
	        $this->db->trans_commit();
	        return 1;
	    } else {
	        $this->db->trans_rollback();
	        //@todo 异常处理部分
	        return 0;
	    }
	}
	
	/**
     * 扣除额度
     */
    function incr_credit($log = array()){
    	$res = self::add_user_amount_log($log);
    	if($res){
    		$credit = self::get_row($log['user_id']);
    		$tmp = array(
    			'user_id' => $log['user_id'],
    			'credit_value' => $log['op'] == 1 ? ($credit['credit_value'] + $log['credit_value']) : ($credit['credit_value'] - $log['credit_value']),
    			'available' => $log['op'] == 1 ? ($credit['available'] + $log['credit_value']) : ($credit['available'] - $log['credit_value']),
    			'op_user' => $log['op_user'],
    			'updatetime' => $log['add_time'],
    			'updateip' => $log['addip'],
    		);
    		$res = self::update_credit($tmp);
    	}
    	return $res;
    }
}